package com.zkingsoft.zkingbase.base;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.widget.Toast;

/**
 * Created by ytf on 2017/3/6 006.
 * Description: 安卓6.0权限申请帮助类,这个类是需要依赖BaseActivity的，调用BaseActivity的bindCurrentPermissionCallback,
 * 达到将callback绑定为当前的callback
 * 所以耦合性还是比较强...
 */

public class PermissionHelper
{
    public static final int FLAG_CONTATCS = 1;
    public static final int FLAG_CAMERA = 2;
    public static final int FLAG_LOCATION = 3;
    public static final int FLAG_PHONE = 4;
    public static final int FLAG_STORAGE = 5;
    public static final int FLAG_SENSORS = 6;
    public static final int FLAG_SMS = 7;
    public static final int FLAG_MICROPHONE = 8;


    private PermissionHelper()
    {
    }

    private static PermissionHelper instance;
    public static PermissionHelper getInstance()
    {
        if(instance == null)
        {
            synchronized (PermissionHelper.class)
            {
                if(instance == null)
                {
                    instance = new PermissionHelper();
                }
            }
        }
        return instance;
    }

    /***
     * 申请联系人权限
     * @param callback
     */
    public void requestContactsPermission(BaseActivity mActivity, PermissionCallback callback)
    {
        mActivity.bindCurrentPermissionCallback(callback);
        if (ContextCompat.checkSelfPermission(mActivity,
                Manifest.permission.READ_CONTACTS)
                != PackageManager.PERMISSION_GRANTED)
        {

            // Should we show an explanation?
            if (ActivityCompat.shouldShowRequestPermissionRationale(mActivity, Manifest.permission.READ_CONTACTS))
            {

                // Show an expanation to the user *asynchronously* -- don't block
                // this thread waiting for the user's response! After the user
                // sees the explanation, try again to request the permission.
                ActivityCompat.requestPermissions(mActivity, new String[]{
                        Manifest.permission.READ_CONTACTS,
                        Manifest.permission.WRITE_CONTACTS,
                        Manifest.permission.GET_ACCOUNTS}, FLAG_CONTATCS);

            } else
            {
                ActivityCompat.requestPermissions(mActivity, new String[]{
                        Manifest.permission.READ_CONTACTS,
                        Manifest.permission.WRITE_CONTACTS,
                        Manifest.permission.GET_ACCOUNTS}, FLAG_CONTATCS);
            }
        }
    }

    /***
     *申请相机权限
     * @param callback
     */
    public void requestCameraPermission(BaseActivity mActivity, PermissionCallback callback)
    {
        mActivity.bindCurrentPermissionCallback(callback);
        if (ContextCompat.checkSelfPermission(mActivity,
                Manifest.permission.CAMERA)
                != PackageManager.PERMISSION_GRANTED)
        {

            // Should we show an explanation?
            if (ActivityCompat.shouldShowRequestPermissionRationale(mActivity, Manifest.permission.CAMERA))
            {

                // Show an expanation to the user *asynchronously* -- don't block
                // this thread waiting for the user's response! After the user
                // sees the explanation, try again to request the permission.
                ActivityCompat.requestPermissions(mActivity, new String[]{Manifest.permission.CAMERA}, FLAG_CAMERA);

            } else
            {
                ActivityCompat.requestPermissions(mActivity, new String[]{Manifest.permission.CAMERA}, FLAG_CAMERA);
            }
        }
    }

    /***
     * 申请位置权限
     * @param callback
     */
    public void requestLocationPermission(BaseActivity mActivity, PermissionCallback callback)
    {
        mActivity.bindCurrentPermissionCallback(callback);
        if (ContextCompat.checkSelfPermission(mActivity,
                Manifest.permission.ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED)
        {

            // Should we show an explanation?
            if (ActivityCompat.shouldShowRequestPermissionRationale(mActivity, Manifest.permission.ACCESS_FINE_LOCATION))
            {

                // Show an expanation to the user *asynchronously* -- don't block
                // this thread waiting for the user's response! After the user
                // sees the explanation, try again to request the permission.
                ActivityCompat.requestPermissions(mActivity, new String[]{
                        Manifest.permission.ACCESS_FINE_LOCATION,
                        Manifest.permission.ACCESS_COARSE_LOCATION}, FLAG_LOCATION);
            } else
            {
                ActivityCompat.requestPermissions(mActivity, new String[]{
                        Manifest.permission.ACCESS_FINE_LOCATION,
                        Manifest.permission.ACCESS_COARSE_LOCATION}, FLAG_LOCATION);
            }
        }
    }

    /***
     * 申请电话权限
     * @param callback
     */
    public void requestPhonePermission(BaseActivity mActivity, PermissionCallback callback)
    {
        mActivity.bindCurrentPermissionCallback(callback);
        if (ContextCompat.checkSelfPermission(mActivity,
                Manifest.permission.CALL_PHONE)
                != PackageManager.PERMISSION_GRANTED)
        {

            // Should we show an explanation?
            if (ActivityCompat.shouldShowRequestPermissionRationale(mActivity, Manifest.permission.CALL_PHONE))
            {

                // Show an expanation to the user *asynchronously* -- don't block
                // this thread waiting for the user's response! After the user
                // sees the explanation, try again to request the permission.
                ActivityCompat.requestPermissions(mActivity, new String[]{
                        Manifest.permission.READ_PHONE_STATE,
                        Manifest.permission.CALL_PHONE,
                        Manifest.permission.USE_SIP,
                        Manifest.permission.PROCESS_OUTGOING_CALLS}, FLAG_PHONE);

            } else
            {
                ActivityCompat.requestPermissions(mActivity, new String[]{
                        Manifest.permission.READ_PHONE_STATE,
                        Manifest.permission.CALL_PHONE,
                        Manifest.permission.USE_SIP,
                        Manifest.permission.PROCESS_OUTGOING_CALLS}, FLAG_PHONE);
            }
        }
    }

    /***
     * 申请sd卡存储权限
     * @param callback
     */
    public void requestStoragePermission(BaseActivity mActivity, PermissionCallback callback)
    {
        mActivity.bindCurrentPermissionCallback(callback);
        if (ContextCompat.checkSelfPermission(mActivity,
                Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED)
        {

            // Should we show an explanation?
            if (ActivityCompat.shouldShowRequestPermissionRationale(mActivity, Manifest.permission.WRITE_EXTERNAL_STORAGE))
            {

                // Show an expanation to the user *asynchronously* -- don't block
                // this thread waiting for the user's response! After the user
                // sees the explanation, try again to request the permission.
                ActivityCompat.requestPermissions(mActivity, new String[]{
                        Manifest.permission.READ_EXTERNAL_STORAGE,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE}, FLAG_STORAGE);

            } else
            {
                ActivityCompat.requestPermissions(mActivity, new String[]{
                        Manifest.permission.READ_EXTERNAL_STORAGE,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE}, FLAG_STORAGE);
            }
        }
    }

    /***
     * 申请短信权限
     * @param callback
     */
    public void requestSMSPermission(BaseActivity mActivity, PermissionCallback callback)
    {
        mActivity.bindCurrentPermissionCallback(callback);
        if (ContextCompat.checkSelfPermission(mActivity,
                Manifest.permission.READ_SMS)
                != PackageManager.PERMISSION_GRANTED)
        {

            // Should we show an explanation?
            if (ActivityCompat.shouldShowRequestPermissionRationale(mActivity, Manifest.permission.READ_SMS))
            {

                // Show an expanation to the user *asynchronously* -- don't block
                // this thread waiting for the user's response! After the user
                // sees the explanation, try again to request the permission.
                ActivityCompat.requestPermissions(mActivity, new String[]{
                        Manifest.permission.READ_SMS,
                        Manifest.permission.SEND_SMS,
                        Manifest.permission.RECEIVE_MMS,
                        Manifest.permission.RECEIVE_SMS,
                        Manifest.permission.RECEIVE_WAP_PUSH}, FLAG_SMS);

            } else
            {
                ActivityCompat.requestPermissions(mActivity, new String[]{
                        Manifest.permission.READ_SMS,
                        Manifest.permission.SEND_SMS,
                        Manifest.permission.RECEIVE_MMS,
                        Manifest.permission.RECEIVE_SMS,
                        Manifest.permission.RECEIVE_WAP_PUSH}, FLAG_SMS);
            }
        }
    }

    public interface PermissionCallback
    {
        void onSuccess();

        void onFailed();
    }
}
